package cn.wuxing.mediation.businessplatform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.wuxing.mediation.businessplatform.domain.annotation.BusinessTag;
import cn.wuxing.mediation.businessplatform.domain.constants.BusinessListConstants;
import cn.wuxing.mediation.businessplatform.domain.dto.DelDTO;
import cn.wuxing.mediation.businessplatform.domain.dto.NotDisputeEventOperateDTO;
import cn.wuxing.mediation.businessplatform.domain.enums.EventCategoryEnum;
import cn.wuxing.mediation.businessplatform.domain.pojo.EventOperateVO;
import cn.wuxing.mediation.businessplatform.domain.vo.BaseBusinessDetailVO;
import cn.wuxing.mediation.businessplatform.domain.vo.NotDisputeEventDetailVO;
import cn.wuxing.mediation.businessplatform.mapper.business.AllCaseDataMapper;
import cn.wuxing.mediation.businessplatform.mapper.business.CmNotDisputeEventMapper;
import cn.wuxing.mediation.businessplatform.mapper.infoconfig.WindowMapper;
import cn.wuxing.mediation.common.core.domain.entity.AllCaseData;
import cn.wuxing.mediation.common.core.domain.entity.CmNotDisputeEvent;
import cn.wuxing.mediation.common.core.domain.entity.IcWindow;
import cn.wuxing.mediation.common.core.redis.RedisCache;
import cn.wuxing.mediation.common.exception.ServiceException;
import cn.wuxing.mediation.common.utils.SecurityUtils;
import cn.wuxing.mediation.common.utils.des.DESUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZoneId;
import java.util.stream.Collectors;

/**
 * @description: <描述>
 * @date: 2024/9/15 21:07
 * @author: yang
 * @version: 1.0
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
@BusinessTag(tag = BusinessListConstants.NOT_DISPUTE_EVENT)
public class NotDisputeEventOperateServiceImpl extends
        BaseBusinessOperateServiceImpl<NotDisputeEventOperateDTO, NotDisputeEventDetailVO> {

    @Autowired
    private CmNotDisputeEventMapper cmNotDisputeEventMapper;

    @Autowired
    private WindowMapper windowMapper;

    @Autowired
    private AllCaseDataMapper allCaseDataMapper;

    @Autowired
    private RedisCache redisCache;

    private static final String PREFIX_CODE = "LF";

    @Override
    Boolean delEvent(DelDTO dto) {
        CmNotDisputeEvent cmNotDisputeEvent = new CmNotDisputeEvent();
        LambdaUpdateWrapper<CmNotDisputeEvent> wrapper = new LambdaUpdateWrapper<>(cmNotDisputeEvent);
        wrapper.in(CmNotDisputeEvent::getId, dto.getIds());
        wrapper.set(CmNotDisputeEvent::getDelFlag, "2");
        wrapper.set(CmNotDisputeEvent::getDelTime, DateTime.now());
        AllCaseData allCaseData = new AllCaseData();
        allCaseData.setDeleted(2);
        LambdaUpdateWrapper<AllCaseData> allCaseDataLambdaUpdateWrapper = Wrappers.lambdaUpdate();
        allCaseDataLambdaUpdateWrapper.in(AllCaseData::getBusinessId, dto.getIds());
        allCaseDataLambdaUpdateWrapper.eq(AllCaseData::getEventCategory, BusinessListConstants.NOT_DISPUTE_EVENT);
        allCaseDataMapper.update(allCaseData, allCaseDataLambdaUpdateWrapper);
        return this.cmNotDisputeEventMapper.update(cmNotDisputeEvent, wrapper) > 0;
    }

    @Override
    EventOperateVO saveEvent(NotDisputeEventOperateDTO notDisputeEventOperateDTO) {
        EventOperateVO vo = new EventOperateVO();
        CmNotDisputeEvent cmNotDisputeEvent = BeanUtil.copyProperties(notDisputeEventOperateDTO,
                CmNotDisputeEvent.class);

        if (null == notDisputeEventOperateDTO.getId()) {
            cmNotDisputeEvent.setEventCode(this.redisCache.getCode(PREFIX_CODE));
            cmNotDisputeEvent.setCreateBy(SecurityUtils.getNickName());
            cmNotDisputeEvent.setRegisterUserId(SecurityUtils.getUserId());
            cmNotDisputeEvent.setCreateTime(DateTime.now());
            cmNotDisputeEvent.setUpdateTime(DateTime.now());
            this.cmNotDisputeEventMapper.insert(cmNotDisputeEvent);
            AllCaseData allCaseData = getAllCaseData(notDisputeEventOperateDTO, cmNotDisputeEvent);
            allCaseDataMapper.insert(allCaseData);
        } else {
            cmNotDisputeEvent.setUpdateBy(SecurityUtils.getNickName());
            cmNotDisputeEvent.setUpdateTime(DateTime.now());

            LambdaUpdateWrapper<CmNotDisputeEvent> updateCmNotDisputeEventWrapper = new LambdaUpdateWrapper<>();
            updateCmNotDisputeEventWrapper.eq(CmNotDisputeEvent::getId, notDisputeEventOperateDTO.getId());
            if ("4".equals(notDisputeEventOperateDTO.getOperateType())) {
                updateCmNotDisputeEventWrapper.set(CmNotDisputeEvent::getWindowId, null);
            }
            this.cmNotDisputeEventMapper.update(cmNotDisputeEvent, updateCmNotDisputeEventWrapper);
           /* cmNotDisputeEvent = this.cmNotDisputeEventMapper.selectById(notDisputeEventOperateDTO.getId());
            AllCaseData allCaseData = getAllCaseData(notDisputeEventOperateDTO, cmNotDisputeEvent);
            LambdaUpdateWrapper<AllCaseData> updateAllCaseDataWrapper = new LambdaUpdateWrapper<>();
            updateAllCaseDataWrapper.eq(AllCaseData::getBusinessId, cmNotDisputeEvent.getId());
            updateAllCaseDataWrapper.eq(AllCaseData::getEventCategory, BusinessListConstants.NOT_DISPUTE_EVENT);
            allCaseDataMapper.update(allCaseData, updateAllCaseDataWrapper);*/
        }
        notDisputeEventOperateDTO.setEventCategory(EventCategoryEnum.NOT_DISPUTE_EVENT.getCode());
        CmNotDisputeEvent notDisputeEvent = cmNotDisputeEventMapper.selectById(cmNotDisputeEvent.getId());
        if (null == notDisputeEvent) {
            throw new ServiceException("未找到事件！");
        }
        vo.setId(notDisputeEvent.getId());
        vo.setEventCode(notDisputeEvent.getEventCode());
        vo.setEventState(notDisputeEvent.getEventState());
        return vo;
    }

    private AllCaseData getAllCaseData(NotDisputeEventOperateDTO dto, CmNotDisputeEvent entity) {
        AllCaseData allCaseData = new AllCaseData();
        allCaseData.setEventCode(entity.getEventCode());
        IcWindow icWindow = windowMapper.selectById(entity.getEventType());
        if (null != icWindow) {
            allCaseData.setEventType(icWindow.getWindowName());
        }
        allCaseData.setEventDesc(entity.getEventDesc());
        allCaseData.setDsr(dto.getApplicantDTOS()==null||dto.getApplicantDTOS().isEmpty()?"":dto.getApplicantDTOS().stream().map(
                applicantDTO -> DESUtil.encrypt(applicantDTO.getName())).collect(Collectors.joining(",")));
        allCaseData.setAreaId(String.valueOf(dto.getAreaId()));
        allCaseData.setInitiateTime(entity.getInitiateTime());
        allCaseData.setSource("智能矛调应用-非纠纷");
        allCaseData.setBusinessId(String.valueOf(entity.getId()));
        allCaseData.setEventCategory("1");
        allCaseData.setBusinessTypeName(EventCategoryEnum.NOT_DISPUTE_EVENT.getDesc());
        return allCaseData;
    }

    @Override
    NotDisputeEventDetailVO getEventInfoDetail(Long eventId) {
        CmNotDisputeEvent cmNotDisputeEvent = this.cmNotDisputeEventMapper.selectById(eventId);

        if (null == cmNotDisputeEvent) {
            return null;
        }
        NotDisputeEventDetailVO notDisputeEventDetailVO = BeanUtil.copyProperties(cmNotDisputeEvent,
                NotDisputeEventDetailVO.class);
        IcWindow icWindow = this.windowMapper.selectById(cmNotDisputeEvent.getEventType());
        if (null != icWindow) {
            notDisputeEventDetailVO.setEventTypeLabel(icWindow.getWindowName());
        }
        notDisputeEventDetailVO.setEventType(null != cmNotDisputeEvent.getEventType() ? String.valueOf(cmNotDisputeEvent.getEventType()) : null);
        notDisputeEventDetailVO.setEventCategory(EventCategoryEnum.NOT_DISPUTE_EVENT.getCode());
        notDisputeEventDetailVO.setRegisterUser(cmNotDisputeEvent.getCreateBy());
        notDisputeEventDetailVO.setRegisterTime(cmNotDisputeEvent.getCreateTime());
        return notDisputeEventDetailVO;

    }

    @Override
    public void checkCloseCase(Long eventId) {
        BaseBusinessDetailVO eventDetail = super.getEventDetail(eventId);
        super.checkApplicant(eventDetail.getApplicantDetailVOList());
    }
}
